package com.example.zhaohang.myphotoread;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private final int     REQ        = 2;
    private final int     IMAGE_CODE = 0;
    private       Context mContext   = this;
    Uri bitmapUri = null;
    private final String    IMAGE_TYPE  = "image/*";
    private       boolean   isSelectPic = false;
    private       ImageView mImageView  = null;
    ;
    private TextView mTextView = null;
    private String mFilePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = (ImageView) findViewById(R.id.imageview);
        mTextView = (TextView) findViewById(R.id.textview);

        mFilePath = Environment.getExternalStorageDirectory().getPath();
        mFilePath = mFilePath + "/" + "temp,png";
    }

    public void button(View view) {
        selectImage();
    }

    private void selectImage() {
        // TODO Auto-generated method stub
        boolean isKitKatO = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        Intent getAlbum;
        if (isKitKatO) {
            getAlbum = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        } else {
            getAlbum = new Intent(Intent.ACTION_GET_CONTENT);
        }
        getAlbum.setType(IMAGE_TYPE);

        startActivityForResult(getAlbum, IMAGE_CODE);


    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode != RESULT_OK) {

            Toast.makeText(this, "ActivityResult resultCode error", Toast.LENGTH_SHORT).show();
            return;

        }
        Bitmap bm = null;
        ContentResolver resolver = getContentResolver();
        if (requestCode == IMAGE_CODE) {
            try {

                Uri originalUri = data.getData();        //获得图片的uri
                bitmapUri = originalUri;
                isSelectPic = true;
                bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);
                //显得到bitmap图片
                mImageView.setImageBitmap(bm);
                String[] proj = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(originalUri, proj, null, null, null);
                if (cursor.moveToFirst()) {
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    String path = cursor.getString(column_index);
                    mTextView.setText(path);
                }
                cursor.close();
            } catch (IOException e) {
                System.out.println(e.toString());
            }


        }

        FileInputStream fileInputStream = null;
        if (requestCode == REQ) {
            Toast.makeText(mContext, "走过了", Toast.LENGTH_LONG).show();
            try {
                fileInputStream = new FileInputStream(mFilePath);
                Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
                mImageView.setImageBitmap(bitmap);
                fileInputStream.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private Bitmap decodeUriAsBitmap(Uri uri) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return bitmap;
    }

    //照相
    public void buttonphoto(View view) {

        //读取内存卡的权限
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);


        } else {
            //            Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
            //            openAlbumIntent.set
        }
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //改变相机的路径
        Uri photoUri = Uri.fromFile(new File(mFilePath));
        //
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(intent, REQ);


    }
}
